package com.motionlog.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.motionlog.R;
import com.motionlog.adapter.MyBaseAdapter;
import com.motionlog.model.Base;
import com.motionlog.model.Database;
import com.motionlog.model.Log;
import com.motionlog.model.Motion;

public class SelectMotion extends Activity {

	private static final String TAG = SelectMotion.class.getSimpleName();

	List<Base> motions = new ArrayList<Base>();
	MyBaseAdapter adapter;

	Database mDb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_motion);

		mDb = Database.getInstance(this);

		Button back = (Button) findViewById(R.id.back);
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		ImageButton add = (ImageButton) findViewById(R.id.new_item);
		add.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				AlertDialog.Builder builder = new Builder(SelectMotion.this);
				builder.setTitle("输入新的情绪");
				View view = SelectMotion.this.getLayoutInflater().inflate(
						R.layout.view_new_motion, null);
				final EditText text = (EditText) view.findViewById(R.id.text);
				builder.setView(view);
				builder.setNegativeButton("取消", null);
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Motion m = new Motion();
								m.name = text.getText().toString();
								m.id = (int) mDb.insertMotionLike("motion",
										m.name);
								android.util.Log.d(TAG, "m.id: " + m.id
										+ ", m.name: " + m.name);
								motions.add(0, m);
								adapter.notifyDataSetChanged();
								// done(m);
							}
						});
				builder.show();
			}
		});

		ListView list = (ListView) findViewById(R.id.motion_list);
		list.setOnItemClickListener(new ListView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Base m = adapter.getItem(position);
				done(m);
			}
		});

		motions.addAll(Database.getInstance(this).getMotions(Motion.class));
		adapter = new MyBaseAdapter(this, motions);
		list.setAdapter(adapter);
	}

	private void done(Base m) {
		Intent i = new Intent();
		i.putExtra("motion", m);
		SelectMotion.this.setResult(RESULT_OK, i);
		// finishActivity(0);
		String className = getIntent().getComponent().getClassName();
		android.util.Log.d(TAG, "name: " + className + ", n: "
				+ StatisticDiagram.class.getName());
		String activity = getIntent().getStringExtra("activity");
		if (activity != null
				&& activity.equals(StatisticDiagram.class.getName())) {
			android.util.Log.d(TAG, "sss");
			StatisticDiagram.motion.id = m.id;
			StatisticDiagram.motion.name = m.name;
			StatisticDiagram.motion.content = m.content;
		}
		SelectMotion.this.finish();
	}
}
